document.addEventListener('DOMContentLoaded', function () 
{
    const taskInput = document.getElementById('taskInput');
    const addButton = document.getElementById('addButton');
    // 获取页面中的无序列表元素，用于展示待办事项
    const taskList = document.getElementById('taskList');
    let taskIndex = 1;/*序号*/

    addButton.addEventListener('click', function () 
    {
        const taskText = taskInput.value;// 获取内容
        if (taskText) 
            {
            const listItem = document.createElement('li');
            listItem.innerHTML = `
                <span>${taskIndex}. ${taskText}</span>
                <button class="deleteButton">删除</button>
                <button class="priorityButton">置顶</button>
                <button class="completeButton">完成</button>
            `;
           
            taskList.appendChild(listItem); // 将列表项添加到无序列表中
            taskInput.value = '';
            taskIndex++;

            // 删除任务
            const deleteButton = listItem.querySelector('.deleteButton');
            deleteButton.addEventListener('click', function () 
            {
                if (confirm('你确定要删除吗?')) 
                {
                    taskList.removeChild(listItem);
                    renumberTasks();
                }
            });

            // 置顶任务
            const priorityButton = listItem.querySelector('.priorityButton');
            priorityButton.addEventListener('click', function () 
            {
                taskList.insertBefore(listItem, taskList.firstChild);
                listItem.classList.add('priority');//
            });

            // 完成任务
            const completeButton = listItem.querySelector('.completeButton');
            completeButton.addEventListener('click', function () 
            {
                listItem.classList.add('completed');
                alert('你真棒 (๑•̀ㅂ•́)و✧');

            });
        }
    });z

    // 重新编号任务
    function renumberTasks() 
    {
        // 获取无序列表中的所有列表项
        const tasks = taskList.querySelectorAll('li');
        taskIndex = 1;
        // 遍历，重新编号
        tasks.forEach
        (task =>{
            task.querySelector('span').textContent = `${taskIndex}. ${task.querySelector('span').textContent.split('. ')[1]}`;
            taskIndex++;
        });
    }
});


